/*
 * Copyright 2012 Marsrutai.lt
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package lt.marsrutai.mobile.android.util;

import java.util.Arrays;
import java.util.Collection;

import lt.marsrutai.mobile.android.util.BeanUtils.BeanProperty;
import android.content.ContentValues;
import android.database.Cursor;

/**
 * 
 * @author jvalskis
 */
public class SQLiteUtils {

    /**
     * Stores a property value to the {@link ContentValues} object.
     * 
     * @param values
     * @param item
     * @param property
     */
    public static <T> void storeProperty(ContentValues values, T item, BeanProperty property) {
        Object value = BeanUtils.getPropertyValue(item, property);
        Class<? extends Object> propertyType = property.getType();

        if (byte.class.isAssignableFrom(propertyType) || Byte.class.isAssignableFrom(propertyType)) {
            values.put(property.getName(), (Byte) value);
        }
        else if (short.class.isAssignableFrom(propertyType) || Short.class.isAssignableFrom(propertyType)) {
            values.put(property.getName(), (Short) value);
        }
        else if (int.class.isAssignableFrom(propertyType) || Integer.class.isAssignableFrom(propertyType)) {
            values.put(property.getName(), (Integer) value);
        }
        else if (long.class.isAssignableFrom(propertyType) || Long.class.isAssignableFrom(propertyType)) {
            values.put(property.getName(), (Long) value);
        }
        else if (float.class.isAssignableFrom(propertyType) || Float.class.isAssignableFrom(propertyType)) {
            values.put(property.getName(), (Float) value);
        }
        else if (double.class.isAssignableFrom(propertyType) || Double.class.isAssignableFrom(propertyType)) {
            values.put(property.getName(), (Double) value);
        }
        else if (boolean.class.isAssignableFrom(propertyType) || Boolean.class.isAssignableFrom(propertyType)) {
            values.put(property.getName(), (Boolean) value);
        }
        else if (char.class.isAssignableFrom(propertyType) || Character.class.isAssignableFrom(propertyType)
                || String.class.isAssignableFrom(propertyType)) {
            values.put(property.getName(), value == null ? null : value.toString());
        }
        else if (propertyType.isEnum()) {
            values.put(property.getName(), value == null ? null : ((Enum<?>) value).name());
        }
        else if (Collection.class.isAssignableFrom(propertyType)) {
            // Support for String collections
            values.put(property.getName(), value == null ? null : StringUtils.join((Collection<?>) value, ","));
        }
        else {
            throw new IllegalArgumentException(String.format("Unsupported property type %s.", propertyType));
        }
    }

    /**
     * Retrieves a property value from the cursor
     * 
     * @param cursor
     * @param beanProperty
     * @return
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static Object retrieveProperty(Cursor cursor, BeanProperty beanProperty) {
        int columnIndex = cursor.getColumnIndex(beanProperty.getName());
        Class<?> propertyType = beanProperty.getType();

        if (cursor.isNull(columnIndex)) {
            return null;
        }
        else if (byte.class.isAssignableFrom(propertyType) || Byte.class.isAssignableFrom(propertyType)) {
            return new Byte((byte) cursor.getInt(columnIndex));
        }
        else if (short.class.isAssignableFrom(propertyType) || Short.class.isAssignableFrom(propertyType)) {
            return cursor.getShort(columnIndex);
        }
        else if (int.class.isAssignableFrom(propertyType) || Integer.class.isAssignableFrom(propertyType)) {
            return cursor.getInt(columnIndex);
        }
        else if (long.class.isAssignableFrom(propertyType) || Long.class.isAssignableFrom(propertyType)) {
            return cursor.getLong(columnIndex);
        }
        else if (float.class.isAssignableFrom(propertyType) || Float.class.isAssignableFrom(propertyType)) {
            return cursor.getFloat(columnIndex);
        }
        else if (double.class.isAssignableFrom(propertyType) || Double.class.isAssignableFrom(propertyType)) {
            return cursor.getDouble(columnIndex);
        }
        else if (boolean.class.isAssignableFrom(propertyType) || Boolean.class.isAssignableFrom(propertyType)) {
            return Boolean.valueOf(cursor.getInt(columnIndex) == 1);
        }
        else if (char.class.isAssignableFrom(propertyType) || Character.class.isAssignableFrom(propertyType)) {
            return cursor.getString(columnIndex).charAt(0);
        }
        else if (String.class.isAssignableFrom(propertyType)) {
            return cursor.getString(columnIndex);
        }
        else if (propertyType.isEnum()) {
            return Enum.valueOf((Class) propertyType, cursor.getString(columnIndex));
        }
        else if (Collection.class.isAssignableFrom(propertyType)) {
            String value = cursor.getString(columnIndex);
            return value == null ? null : Arrays.asList(value.split(","));
        }
        else {
            throw new IllegalArgumentException(String.format("Unsupported property type %s.", propertyType));
        }
    }
}
